package com.aaa.controller.sas;


import com.aaa.entity.LayData;
import com.aaa.entity.MyPage;
import com.aaa.entity.User;
import com.aaa.entity.sas.TSasHuiFang;
import com.aaa.service.sas.TSasHuifangService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("tSashuifang")
public class TSasHuiFangController {
    @Autowired
    private TSasHuifangService tSasHuifangService;
    /**
     * 分页查询所有
     * @param myPage
     * @param
     * @return
     */
    @GetMapping("selectall")
    public LayData selectall(MyPage myPage, TSasHuiFang tSasHuiFang){
        PageInfo selectall = tSasHuifangService.selectall(myPage, tSasHuiFang);
        System.out.println("回访selectall = " + selectall);
        return  new LayData(tSasHuifangService.selectall(myPage,tSasHuiFang));

    }

    @PostMapping("del")
    @ResponseBody
    /**
     * 根据id删除
     */
    public LayData delete(Integer id){
        return new LayData( tSasHuifangService.delete(id));
    }


    /**
     * 修改
     * @param
     * @param session
     * @return
     */

    @PostMapping ("update")
    public LayData update(TSasHuiFang tSasHuiFang, HttpSession session){
        User user = (User) session.getAttribute("user");
        tSasHuiFang.setUpdateby(user.getUsername());
        return new LayData(tSasHuifangService.update(tSasHuiFang));
    }

    /**
     *添加到回访
     * @param request
     * @param session
     * @return
     */
    @PostMapping("add")
    private LayData add(HttpServletRequest request, HttpSession session, TSasHuiFang tSasHuiFang){
        String username = request.getParameter("username");
        System.out.println("11111111111111111111username = " + username);

        Integer id = tSasHuifangService.selectidByusername(username);
        System.out.println("id =============================== " + id);
        Integer rid = tSasHuifangService.selectusernameidByusername(id);
        System.out.println("rid = =========================" + rid);
        if (rid>0){
            System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            return new LayData(false);
        }
        return new LayData(tSasHuifangService.add(request,session,tSasHuiFang));
    }


}
